package io.virtualapp.hook_sdk.firewall;

import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import io.virtualapp.hook_sdk.dao.RiNotesService;
import io.virtualapp.hook_sdk.models.WhitelistAll;
import io.virtualapp.hook_sdk.xml.XStreamUtils;


public class HookSDK {

    public static Context ctx;
    private HookSDK(){xStreamUtils = new XStreamUtils();}
    private static RiNetHooker hooker;
    private XStreamUtils xStreamUtils;
    public void hook(){
        if (FireWallConfig.type == FireWallType.NONE) {
            return;
        }else {
//            Log.e("ZXY","XML解析:"+FireWallConfig.whiteStr);
//            WhitelistAll datas = xStreamUtils.analysisArr(WhitelistAll.class,FireWallConfig.whiteStr);
//            if (datas == null)
//                return;
//            if (datas.Whitelist == null)
//                return;
//            if (datas.Whitelist.size() == 0)
//                return;
//            FireWallConfig.array = new ArrayList<>();
//            for (int i = 0;i < datas.Whitelist.size();i ++){
//                Log.e("ZXY","XML解析:"+"http://"+datas.Whitelist.get(i).url+"/");
//                FireWallConfig.array.add(new AppNetInfo("",
//                        "http://"+datas.Whitelist.get(i).url+"/",""));
//            }
            hooker.initHooker();
        }
    }
    //建造者模式用来进入HookSDK
    public static class Build{
        private HookSDK sdk;
        private HookCallBack callBack;
        /**
         * 创建HookSDK对象
         * */
        public HookSDK build(){
            if (sdk == null) {
                sdk = new HookSDK();
                hooker = new RiNetHooker(callBack);
            }
            return sdk;
        }
        /**
         * 初始化VA操作
         * @param context Application Context
         * */
        public Build init(Context context,String packageName,HookCallBack callBack){
            this.callBack = callBack;
            RiNotesService.getInstance().app_name = packageName;
            ctx = context;
            return this;
        }

        /**
         * 配置黑白名单方法
         * @param type 是否开启黑白名单
         * @param array 名单列表
         * */
        public Build configFireWall(FireWallType type,
                                    List<AppNetInfo> array){
            FireWallConfig.type = type;
            FireWallConfig.array = array;
            return this;
        }

        public Build configHookType(HookType type){
            FireWallConfig.hookType = type;
            return this;
        }

        /**
         *添加Url hook
         * @param baseUrl hook后跳转的url地址
         * */
        public Build hookUrl(String baseUrl){
            FireWallConfig.hookUrl = baseUrl;
            return this;
        }

        /**通过xml方式添加黑白名单的字符串
         * @param whiteStr xml字符串
         * */
        public Build configwhiteStr(String whiteStr){
            FireWallConfig.whiteStr = whiteStr;
            return this;
        }
    }
}
